﻿var browser,WebXdialog;
var alertStyle;
//3:55 2010-4-29 修正FF下不能获取表单已经填写的内容。
function _browser()
{
	this.name="";
	this.version="";
	this.isIE;
	this.isFireFox;
}
browser=new _browser();
WebXdialog=new _Xdialog();

	with(browser)
	{
		version=navigator.appVersion;
		language=navigator.browserLanguage;
	}

	if(navigator.userAgent.indexOf("MSIE")>0) browser.isIE=true;
	if(navigator.userAgent.indexOf("Firefox")>0) browser.isFireFox=true;


function _Xdialog()
{
	this.fielditem;
	this.jsAlert=true;
}

function hiddenObject(status)
{
	var i,elems;
	if (browser.isIE)
		return;
	else
		elems=document.embeds;

	var length=elems.length;
	
	if (status)
	{
		for (i=0;i<length;i++)
		{
			elems[i].setAttribute("_visibility",elems[i].getAttribute("visibility"));
			if (browser.isIE)
				elems[i].filters.gray.enabled = true;
			else
				elems[i].style.visibility="hidden";
			
		}
	}
	else
	{
		for (i=0;i<length;i++)
		{
			if (elems[i].getAttribute("_visibility")!="null");
				elems[i].style.visibility="";
		}
	}
	elems=null;
}


function centerdialog()
{
	WebXdialog.center();
}


_Xdialog.prototype.center=function()
{
	var Xdialog=document.getElementById("Xdialog");
	var Xdialogbackground=document.getElementById("Xdialogbackground");
	var XdialogIframe=document.getElementById("XdialogIframe");
	if (Xdialog!=null)
	{
		with(Xdialog)
		{
			style.left=document.body.scrollLeft+(document.body.clientWidth-Xdialog.clientWidth)/2+"px";
			style.top=(document.body.scrollTop+(document.body.clientHeight-Xdialog.clientHeight)/2-50)+"px";
		}
		Xdialogbackground.style.width=document.body.scrollWidth+"px";
		Xdialogbackground.style.height=document.body.scrollHeight+"px";
		XdialogIframe.style.width=Xdialog.clientWidth+"px";
	}
	Xdialog=null;
	Xdialogbackground=null;
	XdialogIframe=null;
	return;
}
_Xdialog.prototype.show=function (sUrl,sArg,sTitle,sWidth,sHeight)
{
		if (this.jsAlert)
		{
			if (sArg)
			{
				try
				{
					//sArg.scrollIntoView();
					sArg.style.backgroundColor="#FF0000";
				}
				catch(e)
				{}
			}
			alert(sUrl);
			if (sArg)
			{
				try
				{
					sArg.focus();
					sArg.style.backgroundColor="";
				}
				catch(e)
				{}
			}
			return;
		}

		if (sArg!=null)
		{
			fielditem=sArg;
		}
		hiddenObject(1);
		var Xdialogbackground,Xdialog,XdialogTitle,XdialogCloseBtn,XdialogIframe,XdialogBody,XdialogOkBtn,XdialogOkBtnDiv,i,elems;
		if(document.getElementById("Xdialogbackground")==null)
		{
			Xdialogbackground=document.createElement("div");
			XdialogTitle=document.createElement("div");
			Xdialog=document.createElement("div");
			XdialogCloseBtn=document.createElement("div");
			XdialogBody=document.createElement("div");
			
			
			XdialogTitle.id="XdialogTitle";
			document.body.appendChild(Xdialogbackground);
			document.body.appendChild(Xdialog);
			Xdialog.appendChild(XdialogTitle);
			Xdialog.appendChild(XdialogCloseBtn);

			XdialogIframe=document.createElement("iframe");
			XdialogIframe.id="XdialogIframe";
			XdialogIframe.frameBorder="0";
			XdialogIframe.scrolling="no";

			XdialogBody.id="XdialogBody";
			Xdialog.appendChild(XdialogBody);
			
			Xdialog.appendChild(XdialogIframe);

			XdialogOkBtnDiv=document.createElement("div");
			XdialogOkBtnDiv.id="XdialogOkBtnDiv";
			Xdialog.appendChild(XdialogOkBtnDiv);
			XdialogOkBtn=document.createElement("input");
			XdialogOkBtn.id="XdialogOkBtn";
			XdialogOkBtn.type="button";
			XdialogOkBtn.value=" 我知道了 ";
			XdialogOkBtnDiv.appendChild(XdialogOkBtn);
		}
		else
		{
			Xdialogbackground=document.getElementById("Xdialogbackground");
			XdialogTitle=document.getElementById("XdialogTitle");
			Xdialog=document.getElementById("Xdialog");
			XdialogCloseBtn=document.getElementById("XdialogCloseBtn");
			XdialogIframe=document.getElementById("XdialogIframe");
			XdialogBody=document.getElementById("XdialogBody");
			XdialogOkBtn=document.getElementById("XdialogOkBtn");
			XdialogOkBtnDiv=document.getElementById("XdialogOkBtnDiv");
		}
		
		
		with(Xdialogbackground)
		{
			id="Xdialogbackground";
			style.postion="absolute";
			style.zIndex="1";
			style.display="none";

		}
		
		
		with(Xdialog)
		{
			id="Xdialog";
			style.position="absolute";
			style.zIndex="2";
			style.width=sWidth+"px";
			style.display="";	
		}
		
		with(XdialogCloseBtn)
		{
			id="XdialogCloseBtn";
			style.position="absolute";
			style.zIndex="3";
			onclick=function()
			{
				//_Xdialog._close();
				document.getElementById("Xdialogbackground").style.display="none";
				document.getElementById("Xdialog").style.display="none";
				if (typeof(fielditem)=="object")
				{
					try{
						//fielditem.scrollIntoView();
						fielditem.style.backgroundColor="";
						fielditem.focus();
					}
					catch(e)
					{
					}
				}
				hiddenObject(0);
			}
		}
		with(XdialogOkBtn)
		{
			onclick=function()
			{
				//_Xdialog._close();
				document.getElementById("Xdialogbackground").style.display="none";
				document.getElementById("Xdialog").style.display="none";
				if (typeof(fielditem)=="object")
				{
					try
					{
						//fielditem.scrollIntoView();
						fielditem.style.backgroundColor="";
						fielditem.focus();
					}
					catch(e)
					{
					}
				}
				hiddenObject(0);
			}
		}		
		
		if (browser.isIE)
			XdialogTitle.innerText=sTitle;
		else
			XdialogTitle.textContent=sTitle;


		if (sUrl.indexOf("http://")>=0 || sUrl.indexOf("/")==0)
		{
			XdialogOkBtnDiv.style.display="none";
			
			XdialogIframe.contentWindow.document.location=sUrl;
			if (browser.isIE)
				XdialogBody.innerText="";
			else
				XdialogBody.textContent="";
			with (XdialogIframe)
			{
				
				style.left="0px";
				style.top="30px";
				style.width=Xdialog.clientWidth+"px";
				style.height=sHeight+"px";
				style.opacity=1;
				style.filter="alpha(opacity=100)";
			}
			XdialogOkBtn.style.display="none";
		}
		else
		{
			XdialogOkBtnDiv.style.display="";
			if (browser.isIE)
			{
				XdialogBody.innerHTML=sUrl.toString();
			}
			else
			{
				XdialogBody.innerHTML=sUrl.toString();
				hiddenObject(1);
			}
			with (XdialogIframe)
			{
				style.position="absolute";
				style.left="0px";
				style.top="0px";
				style.height=(Xdialog.clientHeight+2)+"px";
				style.width=Xdialog.clientWidth+"px";
				style.opacity=0;
				style.filter="alpha(opacity=0)";
			}
			XdialogOkBtn.style.display="";
		}


		Xdialogbackground.style.display="";
		Xdialog.style.display="";
		this.center();


			with(XdialogOkBtn)
			{
				try{focus();}catch(e){}
				style.position="absolute";
				style.left=((Xdialog.clientWidth-clientWidth)/2)+"px";
				style.top="0px";
			}


		if (browser.isIE)
			document.body.onscroll=function(){centerdialog();};
		else
			window.onscroll=function(){centerdialog();};
		window.onresize=function(){centerdialog()};

	

		document.body.onkeydown=function(event)
		{
			var keycode=(window.event ? window.event.keyCode : event.which);
			
			if(keycode==27)
			{
				document.getElementById("Xdialogbackground").style.display="none";
				document.getElementById("Xdialog").style.display="none";
				if (typeof(fielditem)=="object")
				{
						//fielditem.scrollIntoView();
						fielditem.style.backgroundColor="";
						fielditem.focus();

				}
				hiddenObject(0);
			}
		}
		return;
	}

_Xdialog.prototype._close=function ()
	{
		document.body.onkeydown="";
		document.getElementById("Xdialogbackground").style.display="none";
		document.getElementById("Xdialog").style.display="none";
		if (typeof(this.fielditem)=="object")
		{
			//this.fielditem.scrollIntoView();
			this.fielditem.style.backgroundColor="";
			this.fielditem.focus();
		}
		hiddenObject(0);
		return;

	}

function jsformcheck(evt,_formId)
{
	
	var alerttext,j,elem,checked;
	
	evt = evt ? evt : (window.event ? window.event : null);
	var FormElem;
	var language_formfieldnotnull="请填写[$fieldname] !";
	var language_formcheck="表单验证";
	var language_formfieldmorethanmaxlength="表单项 [$fieldname] 应该小于$maxlength个字符!";
	var language_formfieldnumberrequest="表单项 [$fieldname] 应该填写数字!";
	var language_formfielddaterequest="表单项 [$fieldname] 应该填写日期!";
	
	var FormElems;
	if(_formId)
	{
		FormElem=document.getElementById(_formId);
	}
	else
	{
		if (document.all)
		{
			FormElem=event.srcElement;
		}
		else
		{
			FormElem=evt.target;
		}
	}
	FormElems=FormElem.elements;
	for (var i=0;i<FormElems.length;i++)
	{
		if (FormElems[i].tagName=="INPUT")
		{

			if (!FormElems[i].getAttribute("type") || FormElems[i].getAttribute("type")=="text" || FormElems[i].getAttribute("type")=="password")
			{
					
					FormElems[i].setAttribute("value",FormElems[i].value);
					if (FormElems[i].getAttribute("nullabled")!=null)
					{
						FormElems[i].value=FormElems[i].value.replace(/(^\s*)|(\s*$)/g, "");	//trim

						switch (FormElems[i].getAttribute("datatype"))
						{
							case "string":
								if (FormElems[i].getAttribute("nullabled")=="false")
								{
									if (FormElems[i].value=="")
									{
										//FormElems[i].scrollIntoView();
										FormElems[i].focus();
										alerttext=language_formfieldnotnull;
										alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
										WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
										return false;
									}
									else if(FormElems[i].value.length>FormElems[i].getAttribute("maxlength"))
									{
										//FormElems[i].scrollIntoView();
										FormElems[i].focus();
										alerttext=language_formfieldmorethanmaxlength;
										alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
										alerttext=alerttext.replace("$maxlength",FormElems[i].getAttribute("maxlength"))
										WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
										return false;				
									}
								}
								else
								{
									if(FormElems[i].value.length>FormElems[i].getAttribute("maxlength"))
									{
										//FormElems[i].scrollIntoView();
										FormElems[i].focus();
										alerttext=language_formfieldmorethanmaxlength;
										alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
										alerttext=alerttext.replace("$maxlength",FormElems[i].getAttribute("maxlength"))
										WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
										return false;				
									}
								}
								break;
								
							case "number":
								if (FormElems[i].getAttribute("nullabled")=="false")
								{
									if (!isNumber(FormElems[i].value))
									{
										//FormElems[i].scrollIntoView();
										FormElems[i].focus();
										alerttext=language_formfieldnumberrequest;
										alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
										WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
										return false;
									}

								}
								else
								{
									if (FormElems[i].value!="" && !isNumber(FormElems[i].value))
									{
										alerttext=language_formfieldnumberrequest;
										alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
										WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
										return false;
									}
								}
								break;
							case "date":
								if (FormElems[i].getAttribute("nullabled")=="false")
								{
									if (!isDate(FormElems[i].value))
									{
										//FormElems[i].scrollIntoView();
										//FormElems[i].focus();
										alerttext=language_formfielddaterequest;
										alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
										WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
										return false;
									}

								}
								else
								{
									if (FormElems[i].value!="" && !isDate(FormElems[i].value))
									{
										return false;
									}
								}
								break;					
							
							case "radio":case "checkbox":
								//单选框
								if (FormElems[i].getAttribute("nullabled")=="false")
								{
									checked=false;
									elem=document.getElementsByName(FormElems[i].getAttribute("name"));
									
									for (j=0;j<elem.length;j++)
									{
										if(elem[j].checked)
										{
											checked=true;
											break;
										}
									}
									if (!checked)
									{
										//FormElems[i].scrollIntoView();
										FormElems[i].focus();
										alerttext=language_formfieldnotnull;
										alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
										WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
										return false;
									}
									
								}
								break;
					
						}
				}
			}
			else if (FormElems[i].getAttribute("type")=="radio" || FormElems[i].getAttribute("type")=="checkbox")
			{
				if(FormElems[i].checked)
					FormElems[i].setAttribute("checked",FormElems[i].checked);
				if (FormElems[i].getAttribute("nullabled")=="false")
				{
					checked=false;
					elem=document.getElementsByName(FormElems[i].getAttribute("name"));
					
					for (j=0;j<elem.length;j++)
					{
						if(elem[j].checked)
						{
							checked=true;
							break;
						}
					}
					if (!checked)
					{
						//FormElems[i].scrollIntoView();
						FormElems[i].focus();
						alerttext=language_formfieldnotnull;
						alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
						WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
						return false;
					}
					
				}				
			}
		}
		else if (FormElems[i].tagName=="SELECT")
		{

			//下拉选框
			FormElems[i].options[FormElems[i].selectedIndex].selected=true;
			if (FormElems[i].getAttribute("nullabled")=="false")
			{
							
				if (FormElems[i].value=="")
				{
					//FormElems[i].scrollIntoView();
					alerttext=language_formfieldnotnull;
					alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
					WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
					return false;
				}
							
			}
			
		}
		else if (FormElems[i].tagName=="TEXTAREA")
		{
			FormElems[i].innerText=FormElems[i].value;
			if (FormElems[i].getAttribute("nullabled")=="false")
			{
				
				if (FormElems[i].value=="")
				{
					//FormElems[i].scrollIntoView();
					FormElems[i].focus();
					alerttext=language_formfieldnotnull;
					alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
					WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
					return false;
				}
							
			}
			else
			{
				if(FormElems[i].getAttribute("maxlength"))
				{
					if(FormElems[i].value.length>=FormElems[i].getAttribute("maxlength"))
					{
						//FormElems[i].scrollIntoView();
						FormElems[i].focus();
						alerttext=language_formfieldmorethanmaxlength;
						alerttext=alerttext.replace("$fieldname",FormElems[i].getAttribute("alerttext"))
						alerttext=alerttext.replace("$maxlength",FormElems[i].getAttribute("maxlength"))
						WebXdialog.show(alerttext,FormElems[i],language_formcheck,300,50);
						return false;				
					}
				}
			}
		}

	}
	return true;
}



